package edu.cth.stud.accelerateit.view.android;

import java.beans.PropertyChangeEvent;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
import edu.cth.stud.accelerateit.R;
import edu.cth.stud.accelerateit.view.GUIController;

public class TutorialActivity extends Activity implements IAndroidGUIComponent {
	
	private static String PREFS_NAME = "WordFinderSettings";
	private TextView tutorialText;
	private TextView tutorialTitleText;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// link content, for code structure:
		linkContent();
		
		// load settings that are changed in options
		
		loadSettings();

		// Register this activity/view with the GUIController:
		GUIController.getInstance().registerListener(this);
	}
	
	private void loadSettings(){
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		int textColor = settings.getInt("textColor", Color.WHITE);
		int backgroundColor = settings.getInt("backgroundColor", Color.BLACK);
		tutorialText.setTextColor(textColor);
		tutorialTitleText.setTextColor(textColor);
		this.findViewById(R.id.tutorialScreen).setBackgroundColor(backgroundColor);
		
	}
	
	private void linkContent(){
		setContentView(R.layout.tutorial_view);
		tutorialText = (TextView) this.findViewById(R.id.tutorialText);
		tutorialTitleText = (TextView) this.findViewById(R.id.tutorialTitleText);
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		
		// If the event was a shutdown command
		if (event.getPropertyName().equals("Shutdown")) {
			GUIController.getInstance().unregisterListener(this);
			finish();
		}
	}
	
	@Override
	public void onBackPressed() {
		goBack();
	}
	
	private void goBack() {
		// Required to keep down the activity count:
		finish();
		GUIController.getInstance().unregisterListener(this);

		Intent showMainMenu = new Intent(this.getApplicationContext(),
				WordFinderActivity.class);
		startActivity(showMainMenu);
	}

}
